﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ZSZ.Core.Entities;

namespace ZSZ.EntityFramework.ModelConfig
{
    class HouseConfig:EntityTypeConfiguration<HouseEntity>
    {
        //public HouseConfig()
        //{
        //    ToTable("T_Houses");
        //    HasRequired(h => h.Community).WithMany().HasForeignKey(h => h.CommunityId).WillCascadeOnDelete(false);
        //    HasRequired(h => h.RoomType).WithMany().HasForeignKey(h => h.RoomTypeId).WillCascadeOnDelete(false);
        //    HasRequired(h => h.Status).WithMany().HasForeignKey(h => h.StatusId).WillCascadeOnDelete(false);
        //    HasRequired(h => h.DecorateStatus).WithMany().HasForeignKey(h => h.DecorateStatusId).WillCascadeOnDelete(false);
        //    HasRequired(h => h.Type).WithMany().HasForeignKey(h => h.TypeId).WillCascadeOnDelete(false);
        //    Property(h => h.Address).IsRequired().HasMaxLength(128);
        //    Property(h => h.Description).IsOptional();
        //    Property(h => h.Direction).IsRequired().HasMaxLength(20);
        //    Property(h => h.OwnerName).IsRequired().HasMaxLength(20);
        //    Property(h => h.OwnerPhoneNum).IsRequired().HasMaxLength(20).IsUnicode(false);
        //}

        public override void Map(EntityTypeBuilder<HouseEntity> builder)
        {
            builder.ToTable("T_House");
            builder.Property(h => h.Address).IsRequired().HasMaxLength(128);
            builder.Property(h => h.Description).HasMaxLength(500);
            builder.Property(h => h.Direction).IsRequired().HasMaxLength(20);
            builder.Property(h => h.OwnerName).IsRequired().HasMaxLength(20);
            builder.Property(h => h.OwnerPhoneNum).IsRequired().HasMaxLength(20).IsUnicode(false);

            builder.HasOne(e => e.Community).WithMany().HasForeignKey(h => h.CommunityId)
                .OnDelete(DeleteBehavior.ClientSetNull);
            builder.HasOne(e => e.RoomType).WithMany().HasForeignKey(h => h.RoomTypeId)
                .OnDelete(DeleteBehavior.ClientSetNull);
            builder.HasOne(e => e.Status).WithMany().HasForeignKey(h => h.StatusId)
                .OnDelete(DeleteBehavior.ClientSetNull);
            builder.HasOne(e => e.DecorateStatus).WithMany().HasForeignKey(h => h.DecorateStatusId)
                .OnDelete(DeleteBehavior.ClientSetNull);
            builder.HasOne(e => e.Type).WithMany().HasForeignKey(h => h.TypeId)
                .OnDelete(DeleteBehavior.ClientSetNull);
        }
    }
}
